package net.ksic.uridongne;

import net.ksic.common.NetworkManager;
import net.ksic.common.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {

	TextView idView = null, pwView = null;
	EditText editId = null, editPw = null;
	Button cancel = null, submit = null, signup = null, auto = null;
	
	PreferenceManager preferenceManager;
	NetworkManager networkManager;
	boolean isLogin = false;
	boolean autoLogin = false;
	
	String RESULT = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		networkManager = new NetworkManager();
		preferenceManager = new PreferenceManager(getApplicationContext());
		
		setButton();
	}

	public void setButton() {
		idView = (TextView) findViewById(R.id.idView);
		pwView = (TextView) findViewById(R.id.pwView);

		editId = (EditText) findViewById(R.id.editId);
		editPw = (EditText) findViewById(R.id.editPw);

		cancel = (Button) findViewById(R.id.cancel);
		submit = (Button) findViewById(R.id.submit);
		signup = (Button) findViewById(R.id.signup);
		auto = (Button) findViewById(R.id.auto);

		cancel.setOnClickListener(this);
		submit.setOnClickListener(this);
		signup.setOnClickListener(this);		
		auto.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			Toast.makeText(this, "로그인 취소", Toast.LENGTH_SHORT).show();
			callIntro();
			break;
		case R.id.submit:			
			if (editId.getText().toString().equals("") || editPw.getText().toString().equals("")) {
				Toast.makeText(this, "아이디와 패스워드를 입력해 주세요.", Toast.LENGTH_SHORT).show();
			} else {
				
				//String URL = "http://20.20.20.34:8080/lookdom/testlogin.do";
				String URL = "http://20.20.20.124:8080/MyServer/JSONServer.jsp";
				
				String MSG = editId.getText().toString() + "," + editPw.getText().toString();				
				
				Log.i("url, msg", URL + "?" + MSG);				

				new sendWorker().execute(URL, MSG);			
			}
			break;
		case R.id.auto:
			if (editId.getText().toString().equals("") || editPw.getText().toString().equals("")) {
				Toast.makeText(this, "아이디와 패스워드를 입력해 주세요.", Toast.LENGTH_SHORT).show();
			} else {
				//String URL = "http://20.20.20.34:8080/lookdom/testlogin.do";
				String URL = "http://20.20.20.124:8080/MyServer/JSONServer.jsp";
				
				String MSG = editId.getText().toString() + "," + editPw.getText().toString();				
				
				Log.i("url, msg", URL + "?" + MSG);	
				
				autoLogin = true;
				
				new sendWorker().execute(URL, MSG);			
			}
			break;
		case R.id.signup:
			Toast.makeText(this, "회원가입", Toast.LENGTH_SHORT).show();
			callSign();			
			break;
		}	
	}
	
	private class sendWorker extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {

			RESULT = networkManager.loginJSP(params[0], params[1]);		
			
			return RESULT;
		}
		
		@Override
		protected void onPostExecute(String result) {
			Log.i("RESULT-_-", RESULT);
			if(RESULT.equals("true")) {
				if(autoLogin){
					preferenceManager.enableAutoLogin();
					preferenceManager.setLoginId(editId.getText().toString());
					preferenceManager.setLoginSessionKey("session_key");
					Log.i("자동로그인", editId.getText().toString());
					Log.i("자동로그인", preferenceManager.getLoginId());
					Log.i("자동로그인", preferenceManager.getLoginSessionKey());
				} else {
					preferenceManager.diableAutoLogin();
					preferenceManager.setLoginId(editId.getText().toString());
				}
				callMap();
			} else {
				callIntro();
			}
		}		
	}

	private void callIntro() {
		Intent intent = null;		
		intent = new Intent(this, IntroActivity.class);
		startActivity(intent);		
	}
	
	private void callMap() {
		Intent intent = null;
		intent = new Intent(this, MapActivity.class);
		startActivity(intent);		
	}
	
	private void callSign() {
		Intent intent = null;		
		intent = new Intent(this, SignupActivity.class);
		startActivity(intent);		
	}
}
